package ua.com.minus1.lookbattle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.*;
import android.support.v4.view.ViewPager;
import android.util.Log;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import ua.com.minus1.lookbattle.menu.ClicksActionView;
import ua.com.minus1.lookbattle.utils.AppPreferences;
import ua.com.minus1.lookbattle.utils.BundleValues;

/**
 * Created with IntelliJ IDEA.
 * User: Flash
 * Date: 28.09.12
 * Time: 14:26
 * To change this template use File | Settings | File Templates.
 */
public class LooksActivity extends SherlockFragmentActivity implements
    LooksListFragment.Callbacks, AddLookDialogFragment.Callbacks{

    private int mCatId;
    private ViewPager mViewPager;
    private int mLookId;
    private LooksPagerAdapter mAdapter;
    public static final int ADD_LOOK = 0;
    private static final int SHOW_DETAILS = 1;
    private AppPreferences mPreferences;
    private ClicksActionView mClicksActionView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_looks);

        Bundle extras = getIntent().getExtras();
        if(extras != null) {
            mCatId = extras.getInt(BundleValues.CAT_ID);
        }

        mPreferences = new AppPreferences(this);
        setupViews();
        mViewPager.setCurrentItem(CatItemFactory.getCatPositionId(mCatId), false);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(mClicksActionView != null)
            mClicksActionView.setClicks(mPreferences.getClicks());
    }

    private void setupViews() {
        mViewPager = (ViewPager) findViewById(R.id.looks_pager);
        mAdapter = new LooksPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getSupportMenuInflater().inflate(R.menu.looks, menu);
        mClicksActionView = (ClicksActionView) menu.findItem(R.id.menu_looks_clicks).getActionView();
        mClicksActionView.setClicks(mPreferences.getClicks());
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add_look:
                addLook();
                return true;
            case R.id.menu_refresh:
                getCurrentFragment().refresh();
                return true;
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void addLook() {
/*
        Bundle extras = new Bundle();
        extras.putInt(BundleValues.CAT_ID, CatItemFactory.getCatIdOnPosition(mViewPager.getCurrentItem()));
        Intent intent = new Intent(this, TakePictureActivity.class);
        intent.putExtras(extras);
        startActivityForResult(intent, ADD_LOOK);
*/
        FragmentManager fm = getSupportFragmentManager();
        AddLookDialogFragment addLookDialog = AddLookDialogFragment.newInstance(CatItemFactory.getCatIdOnPosition(mViewPager.getCurrentItem()));
        addLookDialog.show(fm, "add_look_dialog");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case ADD_LOOK:
                    getCurrentFragment().update();
                    break;
                case SHOW_DETAILS:
                    if(data.getBooleanExtra(Intent.EXTRA_DATA_REMOVED, false))
                        getCurrentFragment().update();
                    break;
            }
        }
    }

    private LooksListFragment getCurrentFragment() {
        Log.d(LooksActivity.class.getName(), "current view pager id is: " + mViewPager.getCurrentItem());
        return (LooksListFragment) mAdapter.instantiateItem(mViewPager, mViewPager.getCurrentItem());
    }

    public void onAddLookDialogSelect(boolean useCamera) {
            Bundle extras = new Bundle();
            extras.putBoolean(BundleValues.USE_CAMERA, useCamera);
            extras.putInt(BundleValues.CAT_ID, mCatId);

            Intent intent = new Intent(this, AddLookActivity.class);
            intent.putExtras(extras);
            startActivityForResult(intent, ADD_LOOK);
    }

    public class LooksPagerAdapter extends FragmentStatePagerAdapter {

        CatItemFactory.CatItem[] mCats;

        public LooksPagerAdapter(FragmentManager fm) {
            super(fm);
            mCats = CatItemFactory.createItems(LooksActivity.this);
        }

        @Override
        public Fragment getItem(int i) {
            LooksListFragment fragment = LooksListFragment.newInstance(getItemData(i).catId);
            return fragment;
        }

        public CatItemFactory.CatItem getItemData(int i) {
            return mCats[i];
        }

        @Override
        public int getCount() {
            return CatItemFactory.getCatsCount();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mCats[position].label.toUpperCase();
        }
    }

    public void onLookSelected(int lookId, int position) {
        mLookId = lookId;
        showDetails(position);
    }

    private void showDetails(int position) {
        int catId = mAdapter.getItemData(mViewPager.getCurrentItem()).catId;
        int[] ids = ((LooksListFragment) mAdapter.instantiateItem(mViewPager, mViewPager.getCurrentItem())).getItemsIds();

        Bundle extras = new Bundle();
        extras.putInt(BundleValues.CAT_ID, catId);
        extras.putInt(BundleValues.LOOK_ID, mLookId);
        extras.putIntArray(BundleValues.IDS, ids);
        extras.putInt(BundleValues.POSITION, position);

        Intent intent = new Intent(this, LookDetailsActivity.class);
        intent.putExtras(extras);
        startActivityForResult(intent, SHOW_DETAILS);
    }
}
